package com.yc.utilslib.service;

import android.os.AsyncTask;
import android.text.TextUtils;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.yc.utilslib.asynctask.WebApiRequest;
import com.yc.utilslib.reader.card.Employee;
import com.yc.utilslib.reader.card.UserCard;
import com.yc.utilslib.up.Error;
import com.yc.utilslib.utils.ApiUtils;
import com.yc.utilslib.utils.StringUtils;


/**
 * 作者：TanTun
 * 时间：2017/6/4
 * 邮箱：32965926@qq.com
 * 描述：请求人员信息的服务
 */

public class QueryEmployeeService implements IService<UserCard, Employee> {

    private UserCard mUserCard;
    private OnServiceResultListener<UserCard, Employee> mOnServiceResultListener;
    private OnServiceErrorListener<UserCard> mOnServiceErrorListener;

    /**
     * @param onRequestResultListener 请求结果通知
     */
    public void setOnRequestResultListener(
            OnServiceResultListener<UserCard, Employee> onRequestResultListener) {
        this.mOnServiceResultListener = onRequestResultListener;
    }

    /**
     * @param onRequestErrorListener 请求结果通知
     */
    public void setOnServiceErrorListener(
            OnServiceErrorListener<UserCard> onRequestErrorListener) {
        this.mOnServiceErrorListener = onRequestErrorListener;
    }

    public boolean query(UserCard userCard) {

        boolean ret = false;
        mUserCard = userCard;

        if (userCard != null && userCard.check()) {

            WebApiRequest<Employee, Error> request = new WebApiRequest<>();
            request.setAsyncTaskSuccess(new WebApiRequest.AsyncTaskSuccess<Employee>() {

                @Override
                public Employee parsing(String json) {

                    Employee emp = null;

                    if (!TextUtils.isEmpty(json)) {
                        try {
                            Gson gson = new Gson();
                            emp = gson.fromJson(json,
                                    new TypeToken<Employee>() {
                                    }.getType());
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }

                    return emp;
                }

                @Override
                public void success(Employee emp) {

                    if (emp != null && mOnServiceResultListener != null) {
                        mOnServiceResultListener.onResult(mUserCard, emp);
                    }
                }
            });
            request.setAsyncTaskError(new WebApiRequest.AsyncTaskError<Error>() {
                @Override
                public Error parsing(String str) {

                    Error err = null;

                    if (!TextUtils.isEmpty(str)) {
                        try {
                            Gson gson = new Gson();
                            err = gson.fromJson(str,
                                    new TypeToken<Error>() {
                                    }.getType());
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }

                    return err;
                }

                @Override
                public void error(Error obj) {
                    if (obj != null) {
                        if (obj.getErrors() != null && obj.getErrors().size() > 0) {
                            mOnServiceErrorListener.onError(mUserCard, obj.getErrors().get(0).getMessage());
                        } else {
                            mOnServiceErrorListener.onError(mUserCard, obj.getMessage());
                        }
                    } else {
                        mOnServiceErrorListener.onError(mUserCard, "未知错误");
                    }
                }

                @Override
                public void error(String msg) {
                    mOnServiceErrorListener.onError(mUserCard, msg);
                }
            });

            try {
                request.setMethod("GET");
                request.setHead("accept", "application/json");
                request.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,
                        ApiUtils.API + "/api/up/v6/employees/" +
                                mUserCard.getExtendedInfo().getEmployeeStrId(), StringUtils.EMPTY);
                ret = true;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        return ret;
    }
}
